package cn.itsource.blog.controller;
import cn.itsource.blog.domain.Article;
import cn.itsource.blog.query.ArticleQuery;
import cn.itsource.blog.service.IArticleService;
import cn.itsource.system.annotation.PreAuthorize;
import cn.itsource.until.JsonResult;
import cn.itsource.vo.ArticleVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.junit.platform.commons.function.Try;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文章接口
 * */
@RestController
@RequestMapping("/article")
@Api(tags = "文章接口类") // 用于描述整个API接口的信息，包括API的标题、描述等
public class ArticleController {
    @Autowired
    private IArticleService articleService;

    /**
     * 查询所有
     * */
    @GetMapping
    @ApiOperation(value = "查询所有") // 用于描述单个接口的操作信息，包括接口的HTTP方法、路径、描述等
    @PreAuthorize(name ="查询所有文章",sn ="article:getAll",descs ="查询所有文章")
    public List<Article> getAll(){
        return articleService.getAll();
    }

    /**
     * 查询单个
     * */
    @ApiOperation(value = "查询单个")
    @GetMapping("/{id}")
    @PreAuthorize(name ="查询单个文章",sn ="article:getOne",descs ="查询单个文章") // 自定义注解添加注解里面的内容到数据库中
    public Article getOne(@PathVariable("id")Long id){
        return articleService.getOne(id);
    }

    /**
     * 删除单个
     * */
    @ApiOperation(value = "删除单个")
    @DeleteMapping("/{id}")
    @PreAuthorize(name ="删除单个文章",sn ="article:delete",descs ="删除单个文章")
    public JsonResult delete(@PathVariable("id")Long id){
        try {
            articleService.delete(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 添加或修改
     * */
    @ApiOperation(value = "添加或修改")
    @PutMapping
    @PreAuthorize(name ="添加或修改单个文章",sn ="article:addOrUpdate",descs ="添加或修改单个文章")
    public JsonResult addOrUpdate(@RequestBody Article article){
        try {
            if (article.getId() == null) {
                articleService.add(article);
            }else {
                articleService.update(article);}
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 批量删除
     * */
    @ApiOperation(value = "批量删除")
    @PatchMapping
    @PreAuthorize(name ="批量删除文章",sn ="article:patchDelete",descs ="批量删除文章")
    public JsonResult patchDelete(@RequestBody Long[] ids){
        try {
            articleService.patchDelete(ids);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 分页查询+高级查询
     * */
    @ApiOperation(value = "分页查询+高级查询")
    @PostMapping
    @PreAuthorize(name ="分页展示文章",sn ="article:page",descs ="分页展示文章")
    public PageInfo<Article> pageQuery(@RequestBody ArticleQuery query){
        return articleService.pageQuery(query);
    }

    /**
     * 根据文章类型查询文章数量
     * */
    @GetMapping("/countNumberByType")
    @ApiOperation(value = "根据文章类型查询文章数量") // 用于描述单个接口的操作信息，包括接口的HTTP方法、路径、描述等
    public List<ArticleVo> countNumberByType(){ // 返回的多个对象所以用list来接
        return articleService.countNumberByType();
    }

    /**
     * 根据文章发布时间查询文章数量
     * */
    @GetMapping("/countNumberByTime")
    @ApiOperation(value = "根据文章乐行查询文章数量") // 用于描述单个接口的操作信息，包括接口的HTTP方法、路径、描述等
    public List<ArticleVo> countNumberByTime(){ // 返回的多个对象所以用list来接
        return articleService.countNumberByTime();
    }

    /**
     * 获取相关文章
     * */
    @GetMapping("/getRelatedArticles/{id}")
    @ApiOperation(value = "获取相关文章") // 用于描述单个接口的操作信息，包括接口的HTTP方法、路径、描述等
    public List<Article> getRelatedArticles(@PathVariable("id")Long id){ // 返回的多个对象所以用list来接
        return articleService.getRelatedArticles(id);
    }

     /**
     * 获取文章分类下的文章
     * */
    @GetMapping("/getArticles/{id}")
    @ApiOperation(value = "获取文章分类下的文章") // 用于描述单个接口的操作信息，包括接口的HTTP方法、路径、描述等
    public ArticleVo getArticles(@PathVariable("id")Long id){ // 返回的多个对象所以用list来接
        return articleService.getArticles(id);
    }

    /**
     * 获取推荐文章
     * */
        @GetMapping("/getArticleRecommend")
    public List<Article> getArticleRecommend() {
        return articleService.getArticleRecommend();
    }

    /**
     * 获取推荐文章数量
     * */
    @GetMapping("/getArticleRecommendCount")
    public JsonResult getArticleRecommendCount() {
        try {
            Integer count = articleService.getArticleRecommendCount();
            return JsonResult.success(count);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }





}
